<?php

/**
 * Controller_Index
 *
 * @auther		FreeYadmin <FreeYadmin@gmail.com>
 * @update		none
 * @package		Action
 */	
class Controller_Category extends Common_Controller
{
	/**
	* method_note
	*	
	* @param	$var		type		note
	* @return	$ret		type		note
	*/
	protected function _preDispatch()
	{
		parent::_preDispatch();
		$this->_view->set('layout_name', 'category.php');

		$left = array(
			'parent' => 'link',
			'index' => 'link/link',
			'sub' => array( 'add', 'list', ),
		);
		$this->_view->assign('left', $left);
	}

	/**
	* method_note
	*	
	* @param	$var		type		note
	* @return	$ret		type		note
	*/
	protected function _indexAction()
	{
		$model = new Model_Category;
		$this->getView()->assign('category_list', $model->getAllList());
	}

	/**
	* method_note
	*	
	* @param	$var		type		note
	* @return	$ret		type		note
	*/
	protected function _addAction()
	{
		$form = new Form_Category;
		if ($this->getRequest()->isPost() && !$form->submit($this->getRequest()->getPost())) {
			$model = new Model_Category;
			$model->insert($form->data());
			$form->setData(null);
		}

		$category_form = $form->getForm();	
		$this->getView()->assign('category_form', $category_form);
	}

	/**
	* method_note
	*	
	* @param	$var		type		note
	* @return	$ret		type		note
	*/
	protected function _editAction()
	{
		$form = new Form_Category;
		$form->action('edit');
		$id = intval($this->getRequest()->getParam('id'));
		
		if (!$id) {
			$this->msg('id is empty.');
		}

		$model = new Model_Category;
		if (!($data = $model->select($id))) {
			$this->msg("id {$id} data is exists");
		}

		$form->setData($data[0]);
		if ($this->getRequest()->isPost() && !$form->submit($this->getRequest()->getPost())) {
			$model->update($form->data(), $id);
			$form->setData(null);
		}

		$category_form = $form->getForm();	
		$this->getView()->assign('category_form', $category_form);
	}

	/**
	* method_note
	*	
	* @param	$var		type		note
	* @return	$ret		type		note
	*/
	protected function _delAction()
	{
		$id = intval($this->getRequest()->getParam('id'));
		
		if (!$id) {
			$this->msg('id is empty.');
		}

		$model = new Model_Category;
		if (!($data = $model->select($id))) {
			$this->msg("id {$id} data is exists");
		}
		if ($model->delete($id)) {
			$this->msg("delete successful id is {$id}");
		}
	}

}//Action end**********
//file end--------------------------------------------------------->